import * as index from '@/api/index.js'
export default {
	data() {
		return {
			tabsList: [{
					id: '',
					name: '全部',
					text:''
				},
				{
					id: 1,
					name: '待支付',
					text:'待支付'
				},
				{
					id: 2,
					name: '已支付',
					text:'已支付'
				},

			],
			form: {
				page: 1,
				status: '',
			},
			loading: false,
			list: [],
			tabsIndex: 0,
			token:'',
			loginForm: {
				code: '',
				mobilecode: ''
			},
		}
	},

	onShow() {
		this.token = uni.getStorageSync('token')
	},
	onLoad(option) {
		let that = this
		this.getwxCode()
		uni.$on('update', function(data) {
			that.form.page = 1
			that.list = []
			that.tabsIndex = 0
			that.getOrderList()
		})
		if(uni.getStorageSync('token')){
			this.getOrderList()
		}
	},
	onPageScroll(){
		this.form.page += 1
		this.getOrderList()
	},
	onPullDownRefresh(){
		this.form.page = 1
		this.list = []
		this.getOrderList()
	},
	methods: {
		tableChange(item, index) {
			if (this.tabsIndex != index) {
				this.form.page = 1
				this.list = []
				this.tabsIndex = index
				this.form.status = item.id
				this.getOrderList()
			}
		},
		//订单列表
		getOrderList() {
			this.loading = false
			index.getOrderList(this.form).then(res => {
				this.list.push(...res.data.data)
				this.loading = true
			})
		},
		//支付
		goPay(item) {
			let that = this
			index.goPay({
				order_id: item.id
			}).then(res => {
				uni.requestPayment({
					provider: 'wxpay',
					timeStamp: res.data.timeStamp,
					nonceStr: res.data.nonceStr,
					package: res.data.package,
					signType: res.data.signType,
					paySign: res.data.paySign,
					success: res => {
						that.$tools.toast('支付成功')
						uni.$emit('update', {
							msg: '页面更新'
						})
					},
					fail: err => {
						that.$tools.toast('支付失败')
					}
				});
			})
		},
		//取消订单
		cancelOrder(item) {
			uni.showModal({
				title: '提示',
				content: '是否确定取消订单?订单取消后不可撤销。',
				success: function(res) {
					if (res.confirm) {
						index.cancelOrder({
							order_id: item.id
						}).then(res => {
							uni.$emit('update', {
								msg: '页面更新'
							})
						})
					} else if (res.cancel) {
					}
				}
			});

		},
		getwxCode() {
			let that = this
			uni.login({
				provider: 'weixin',
				onlyAuthorize: true,
				success: function(loginRes) {
					that.loginForm.code = loginRes.code
				}
			});
		},
		getPhoneNumber(e) {
			this.loginForm.mobilecode = e.detail.code
			this.wxLogin()
		},
		wxLogin() {
			let that = this
			if (this.loginForm.mobilecode && this.loginForm.code) {
				index.wxmobile(this.loginForm).then(res => {
					// if (res.data.userinfo.avatar == '' || res.data.userinfo.nickname == '') {
					// 	that.$tools.navigate('/pages/user/userinfo/index')
					// }
					uni.setStorage({
						key: 'userInfo',
						data: res.data.userinfo,
						success: function() {
						}
					})
					uni.setStorage({
						key: 'token',
						data: res.data.userinfo.token,
						success: function() {
							that.getwxCode()
							that.token = res.data.userinfo.token
							that.getOrderList()
						}
					});
				})
			}
		},
		goDetail(item){
			this.$tools.navigate('/pages/orderInfo/index?id='+item.id)
		}
	}
}